package cn.b.sky.mes.mm.controller;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.enums.DistributionStatus;
import cn.b.sky.mes.mm.model.Distribution;
import cn.b.sky.mes.mm.model.Requirement;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.DistributionCondition;
import cn.b.sky.mes.mm.service.DistributionService;
import cn.b.sky.mes.mm.service.RequirementdistributionService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

/**
 * 
 * <strong>Title : DistributionController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>配送需求台账控制器<br> 
 * <strong>Create on : 2016-9-22 上午10:51:26<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/distribution/*")
public class DistributionController extends SecurityController {

  @Autowired
  DistributionService distributionService;
  
  @Resource
  ECodeService eCodeService;
  
  @Resource
  RequirementdistributionService requirementdistributionService;
  
  @RequestMapping("selectDistributionList")
  public String selectDistributionListByCondition(String navTabId, Model model,@ModelAttribute("condition")DistributionCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setOrderField("CreateDateTime");
    List<Distribution> list = distributionService.selectDistributionListByCondition(condition);
    int count = distributionService.selectDistributionCountByCondition(condition);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.DISTRIBUTION_STATUS.getCode(),
        SecurityContextUtil.getLanguageCode());
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("ecodes", ecode);
    model.addAttribute("page", condition);
    return "mm/distribution/distribution_list";
  }

  @RequestMapping("viewDistribution")
  public String viewDistribution(String navTabId, Model model,String distributionNum) {
    Distribution distribution = distributionService.getDistributionByNum(getSiteCode(), distributionNum);
    List<Requirement> requirements = requirementdistributionService.selectDistributiontProductionByDistributionNum(getSiteCode(), distributionNum);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.DISTRIBUTION_STATUS.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("requirement", requirements.get(0));
    model.addAttribute("requirements", requirements);
    model.addAttribute("ecodes", ecode);
    model.addAttribute("distribution", distribution);
    model.addAttribute(NAVTABID, navTabId);
    return "mm/distribution/distribution_view";
  }
  
  @RequestMapping("confirmDistribution")
  public String confirmDistribution(String navTabId, Model model,String distributionNum) {
    model.addAttribute("distributionNum", distributionNum);
    model.addAttribute(NAVTABID, navTabId);
    return "mm/distribution/distribution_confirm";
  }
  
  @RequestMapping("confirmServedDistribution")
  @ResponseBody
  public DWZResult confirmServedDistribution(String navTabId, Model model,Distribution distribution) {
    Double actualQuantity  = distribution.getActualQuantity();
    Distribution distributions = distributionService.getDistributionByNum(getSiteCode(), distribution.getDistributionNum());
    if(actualQuantity>distributions.getDistributionQuantity() - distributions.getActualQuantity()){
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          "failedOp.M,notActualQuantity.M"));
    }    
    distributionService.confirmServedDistribution(getSiteCode(),distribution.getDistributionNum(),actualQuantity);
    return dialogAjaxDone(navTabId, null, null, null);
  }
  
  @RequestMapping("checkedButtonEnable")
  @ResponseBody
  public DWZResult checkedButtonEnable(String distributionNum) {
    Distribution distributions = distributionService.getDistributionByNum(getSiteCode(), distributionNum);
    if (!distributions.getStatusECode().equals(DistributionStatus.CREATED.getStatus())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          "DistributionStatus.M"));
    }
    return ajaxDone(null);
  }
  
  @RequestMapping("cancelDistribution")
  @ResponseBody
  public DWZResult cancelDistribution(String navTabId, Model model,String distributionNum) {
    if (StringUtils.isBlank(distributionNum)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    Distribution distributions = distributionService.getDistributionByNum(getSiteCode(), distributionNum);
    if (!distributions.getStatusECode().equals(DistributionStatus.CREATED.getStatus())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(),
          "DistributionStatus.M"));
    }
    distributionService.cancelDistribution(getSiteCode(),distributionNum);
    return ajaxDone(null);
  }
  
}
